package kr.hyunist.androidcomplete01saeildamil.ch08;

import java.util.*;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;

public class FreeLine extends Activity {
	private MyView mv;

	public class Vertex {
		float x;
		float y;
		boolean Draw;

		Vertex(float ax, float ay, boolean ad) {
			x = ax;
			y = ay;
			Draw = ad;
		}
	}

	ArrayList<Vertex> arVertex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mv = new MyView(this);
		setContentView(mv);

		arVertex = new ArrayList<Vertex>();
	}

	class MyView extends View {
		Paint p;

		public MyView(Context context) {
			super(context);

			p = new Paint(Paint.ANTI_ALIAS_FLAG);
			p.setColor(Color.BLACK);
			p.setStrokeWidth(3);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawColor(Color.LTGRAY);
			
			for(int i = 0; i < arVertex.size(); i++){
				if(arVertex.get(i).Draw){
					canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y, arVertex.get(i).x, arVertex.get(i).y, p);
				}
			}
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				arVertex.add(new Vertex(event.getX(), event.getY(), false));
				return true;
			}
			
			if(event.getAction() == MotionEvent.ACTION_MOVE){
				arVertex.add(new Vertex(event.getX(), event.getY(), true));
				invalidate();
				return true;
			}
			
			return false;
		}
	}
}
